package de.baumann.browser.model

import de.baumann.browser.api.net.CardApiService
import de.baumann.browser.api.net.OrderApiService
import de.baumann.browser.api.net.base.ApiBuilder
import de.baumann.browser.api.net.vo.*
import de.baumann.browser.rx.RxSchedulers
import de.baumann.browser.utils.Constants.HTTP_OK
import de.baumann.browser.utils.put
import io.reactivex.Observable

class CardModel : BaseModel() {
    private val cardApiService = ApiBuilder.buildService(CardApiService::class.java)

    fun getUserRole(): Observable<Result<Role>> {
        return cardApiService.getUserRole()
                .map {
                    if (it.code == HTTP_OK) {
                        put("userRole", it.data.type)
                    }
                    return@map it
                }
                .compose(RxSchedulers.ioMain())
    }

    fun getCards(): Observable<Result<List<Card>>> {
        return cardApiService.getCards().compose(RxSchedulers.ioMain())
    }

    fun getMyCard(): Observable<Result<MyCard>> {
        return cardApiService.getMyCard().compose(RxSchedulers.ioMain())
    }

    fun callAfterBuy(cardId: String, hash: String): Observable<Result<Any>> {
        return cardApiService.callAfterBuy(cardId, hash)
    }

    fun createCardOrder(id: String, price: String): Observable<Result<PayParam>> {
        val orderApiService = ApiBuilder.buildService(OrderApiService::class.java)
        return orderApiService.crateOrder("6", 7, 1, price, price, id, "", "", "")
                .compose(RxSchedulers.ioMain())
    }

}